import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { User } from '../user/entities/user.entity';
import { Profile } from '../user/entities/profile.entity';
import { Role } from '../role/entities/role.entity';
import { Permission } from '../permission/entities/permission.entity';
import { Widget } from '../permission/entities/widget.entity';

/** 数据库模块 */

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          type: 'mysql',
          autoLoadEntities: true,
          host: process.env.DB_HOST || configService.get('DB_HOST'),
          port: +process.env.DB_PORT || configService.get('DB_PORT'),
          username: process.env.DB_USER || configService.get('DB_USER'),
          password: process.env.DB_PWD || configService.get('DB_PWD'),
          database: process.env.DB_DATABASE || configService.get('DB_DATABASE'),
          synchronize: process.env.NODE_ENV === 'production' ? false : configService.get('DB_SYNC'),
          timezone: '+08:00',
          entities: [User, Profile, Role, Permission, Widget],
          // entities: [__dirname + '/../**/*/.entity{.ts,.js}'],
        };
      },
    }),
  ],
})
export class DbConfigModule {}
